/*
 * Copyright (C) 2010 The Android Open Source Project 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

package org.lhlzi.ithink;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;

public class Utils {
	

	public static final String TAG = Utils.class.toString();

    public static final String[] DEFAULT_12H_DATE_FORMATS = {
            "h:mm AA",
            "EEE h:mm AA",
            "EEE dd/MM",
            "dd/MM/yyyy"
    };
    
    public static final String[] DEFAULT_24H_DATE_FORMATS = {
            "hh:mm",
            "EEE hh:mm ",
            "EEE dd/MM",
            "dd/MM/yyyy"
    };
    
    /**
     * Format a date depending on the difference between today's date and it. 
     * The result will be formatted depending on the date difference: today, this/last week, or older
     * @param formats the three format strings to use (today, this/last week, older)
     * @param milliseconds the date in milliseconds
     * @return the date formatted according to the date difference
     */
    public static CharSequence formatDate(String formats[], long milliseconds) {
            // we just want date, not time  
            Calendar today = Calendar.getInstance();                
            today.set(Calendar.MILLISECOND, 0);
            today.set(Calendar.SECOND, 0);
            today.set(Calendar.MINUTE, 0);
            today.set(Calendar.HOUR_OF_DAY, 0);
            
            Calendar posted = new GregorianCalendar();
            posted.setTimeInMillis(milliseconds);
            posted.set(Calendar.MILLISECOND, 0);
            posted.set(Calendar.SECOND, 0);
            posted.set(Calendar.MINUTE, 0);
            posted.set(Calendar.HOUR_OF_DAY, 0);
            
            long daysDiff = (today.getTimeInMillis() - posted.getTimeInMillis()) / (24*60*60*1000);
            int dayOfWeek = today.get(Calendar.DAY_OF_WEEK);
            
            if (daysDiff <= 7 + dayOfWeek) {
                    if (daysDiff == 0) {
                    	return DateFormat.format(formats[0], milliseconds);
                    } else if (daysDiff <= dayOfWeek) {
                    	return DateFormat.format(formats[1], milliseconds);
                    } else {
                    	return DateFormat.format(formats[2], milliseconds);
                    }
            } else {
                    return DateFormat.format(formats[3], milliseconds);
            }
    }
    
    /**
     * See {@link Utils#formatDate(String[], long)} for details.
     */
    public static CharSequence formatDate(String formats[], Date date) {
            return formatDate(formats, date.getTime());
    }
    
    
    public static CharSequence formatNumber(CharSequence number) {
    	
    	HashMap<String, String> states = DataCacheWrapper.getStates();
    	
    	String phone = number.toString();
    	
    	if (number.length() < 13 && states != null) {
    		if (number.length() > 3 && states.containsKey(phone.substring(0, 3))) {
    			return "(" + phone.substring(0, 3) + ")" + phone.substring(3);
    		} else if (number.length() > 4 && states.containsKey(phone.substring(0, 4))) {
    			return "(" + phone.substring(0, 4) + ")" + phone.substring(4);
    		} else if (number.length() > 5 && states.containsKey(phone.substring(0, 5))) {
    			return "(" + phone.substring(0, 5) + ")" + phone.substring(5);
    		}
    	}
    	
    	return number;
    }
    
    public static boolean isnumber(CharSequence dest) {
    	return Pattern.compile("^[0-9]*$").matcher(dest).find();
    }
    
    public static boolean checkPackage(Context context, String pkg) {
    	
    	if (pkg == null || "".equals(pkg)) return false;
    	
    	try {
    		
    		ApplicationInfo info = context.getPackageManager().getApplicationInfo(
    				pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
    		
    		Log.i(TAG, info.toString());
    		
    		return true;
    	} catch(NameNotFoundException e) {
    		return false;
    	}
    		
    }
    
    public static final String ITHINK = "org.lhlzi.icaller";
    public static final String PRO_PLUGIN = "org.lhlzi.ithink";
    
    public static boolean isInProMode(Context context) {
    	return context.getPackageManager().checkSignatures(ITHINK, PRO_PLUGIN) == PackageManager.SIGNATURE_MATCH;
    }
        
       
}
